package com.coder520.sync_learning.demo.atomic;

import java.util.Date;
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;


public class AtomicIntegerFieldUpdaterTest {
	public static AtomicIntegerFieldUpdater<User> atomicIntegerFieldUpdater=AtomicIntegerFieldUpdater.newUpdater(User.class, "id");
	public static void main(String[] args) {
		User conan=new User(1, "柯南", "1234557", new Date());
//		随便更新一个属性，但还是会输出原来的属性
		System.out.println(atomicIntegerFieldUpdater.getAndIncrement(conan));
		System.out.println(atomicIntegerFieldUpdater.get(conan));
	}
	
	public static  class User {
		public volatile int id;
		private String userName;
		private String password;
		private Date birthday;
		public int getId() {
			return id;
		}
		public void setId(int id) {
			this.id = id;
		}
		public String getUserName() {
			return userName;
		}
		public void setUserName(String userName) {
			this.userName = userName;
		}
		public String getPassword() {
			return password;
		}
		public void setPassword(String password) {
			this.password = password;
		}
		public Date getBirthday() {
			return birthday;
		}
		public void setBirthday(Date birthday) {
			this.birthday = birthday;
		}
		public User(int id, String userName, String password, Date birthday) {
			this.id = id;
			this.userName = userName;
			this.password = password;
			this.birthday = birthday;
		}
		@Override
		public int hashCode() {
			final int prime = 31;
			int result = 1;
			result = prime * result + ((birthday == null) ? 0 : birthday.hashCode());
			result = prime * result + id;
			result = prime * result + ((password == null) ? 0 : password.hashCode());
			result = prime * result + ((userName == null) ? 0 : userName.hashCode());
			return result;
		}
		@Override
		public boolean equals(Object obj) {
			if (this == obj)
				return true;
			if (obj == null)
				return false;
			if (getClass() != obj.getClass())
				return false;
			User other = (User) obj;
			if (birthday == null) {
				if (other.birthday != null)
					return false;
			} else if (!birthday.equals(other.birthday))
				return false;
			if (id != other.id)
				return false;
			if (password == null) {
				if (other.password != null)
					return false;
			} else if (!password.equals(other.password))
				return false;
			if (userName == null) {
				if (other.userName != null)
					return false;
			} else if (!userName.equals(other.userName))
				return false;
			return true;
		}
	}
}
